import { Component, OnInit } from '@angular/core';

import { ActivatedRoute } from '@angular/router';
import { NzModalService, NzMessageService } from 'ng-zorro-antd';

import { ApplicationService } from 'src/app/core/service/application.service';

import { CreateApplicationComponent } from './create-application/create-application.component';
import { UploadWarComponent } from './upload-war/upload-war.component';
import { UploadDependCheckComponent } from './upload-depend-check/upload-depend-check.component';

@Component({
  selector: 'app-app-list',
  templateUrl: './app-list.component.html',
  styleUrls: ['./app-list.component.scss']
})
export class AppListComponent implements OnInit {
  title = '';
  projectUuid = '';
  overviewAppData = {}; // 应用首页总数
  overviewAppMark = ''; //
  allChecked = false;
  indeterminate = false;
  displayData = [];

  pageIndex = 1;      // 页码标识
  pageSize = 10;      // 每页展示多少数据 —— 10条
  total = 1;          // 总共1页

  dataSet = [];       // 数据集合

  loading = true;     // 加载
  sortValue = null;   // 种类值
  sortKey = null;     // 种类关键值
  searchGenderList: string[] = [];



  sort(sort: { key: string, value: string }): void {
    this.sortKey = sort.key;
    this.sortValue = sort.value;
  }

  constructor(
    private applicationService: ApplicationService,
    private message: NzMessageService,
    private modalService: NzModalService,
    private activateRoute: ActivatedRoute
  ) { }

  /**
   * 获取应用列表信息
   * @param reset 获取数据
   */
  getApplicationPage(reset: boolean = false): void {
    console.log('=== getApplicationPage() ===>coming in...');
    if (reset) {
      this.pageIndex = 1;
    }
    this.applicationService.getApplicationPage(this.pageIndex, this.pageSize, this.projectUuid)
      .subscribe((applicationPage) => {
        this.dataSet = applicationPage.data;
        this.total = applicationPage.total;
      });
  }

  updateFilter(value: string[]): void {
    this.searchGenderList = value;
  }

  currentPageDataChange($event: Array<{ name: string; age: number; address: string; checked: boolean; disabled: boolean; }>): void {
    this.displayData = $event;
    this.refreshStatus();
  }


  /**
   * 刷新状态
   */
  refreshStatus(): void {
    const allChecked = this.displayData.filter(value => !value.disabled).every(value => value.checked === true);
    const allUnChecked = this.displayData.filter(value => !value.disabled).every(value => !value.checked);
    this.allChecked = allChecked;
    this.indeterminate = (!allChecked) && (!allUnChecked);
  }

  /**
   * 全选方法
   */
  checkAll(value: boolean): void {
    this.displayData.forEach(data => {
      if (!data.disabled) {
        data.checked = value;
      }
    });
    // 刷新状态
    this.refreshStatus();
  }
  /**
   * 创建应用后刷新 overview组件
   */
  refreshOverview() {
    this.applicationService.appCount().subscribe((appData) => {
        if (appData.status === 1) {
          this.overviewAppMark = 'app';
          this.overviewAppData = {
            appCount: appData.data.appCount,
            jarCount: appData.data.jarCount,
            cveCount: appData.data.cveCount,
            licenseCount: appData.data.licenseCount
          };
        }
      });
}
  /**
   * 创建应用的模态框
   */
  createApplicationModal(): void {
    const modal = this.modalService.create({
      nzTitle: '创建应用',
      nzContent: CreateApplicationComponent,
      nzComponentParams: {
      },
      nzFooter: null
    });
    modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!'));
    modal.afterClose.subscribe(result => {
      this.getApplicationPage();
      this.refreshOverview();
    });
  }
  

  /**
   * 上传war
   */
  uploadWarModal(appUuid: string): void {
    const modal = this.modalService.create({
      nzTitle: '上传war',
      nzContent: UploadWarComponent,
      nzComponentParams: {
        appUuid: appUuid
      },
      nzFooter: null
    });
    modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!'));
    modal.afterClose.subscribe(result => {
      this.getApplicationPage();
    });
  }

  /**
   * 上传dependence-check报告
   */
  uploadDependCheckModal(appUuid: string): void {
    const modal = this.modalService.create({
      nzTitle: '上传dependence-check报告',
      nzContent: UploadDependCheckComponent,
      nzComponentParams: {
        appUuid: appUuid
      },
      nzFooter: null
    });
    modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!'));
    modal.afterClose.subscribe(result => {
      this.getApplicationPage();
    });
  }

  /**
   * 删除application
   */
  deleteApp(appUuid: string) {
    console.log('=== Janine.deleteApp.appUuid ===>', appUuid);
    this.applicationService.deleteApp(appUuid).subscribe(deleteData => {
      if (deleteData.status === 1) {
        this.message.success('删除成功!');
        this.getApplicationPage();
      }
    });
  }

  ngOnInit(): void {
    this.activateRoute.queryParams.subscribe(params => {
      this.title = params.title;
      this.projectUuid = params.projectUuid;
      console.log('=== Janine.activateRoute.title ==???=>', this.title);
      console.log('=== Janine.activateRoute.projectUuid ==???=>', this.projectUuid);
      this.getApplicationPage();
    });
    if (!this.projectUuid) {
      this.title = this.activateRoute.snapshot.data.title;
      console.log('=== Janine.title ===>', this.title);
    }
  }

}

